import Vue from "vue";

//配置路由相关信息
import VueRouter from "vue-router";
import {Message} from 'element-ui'
import store from "../store/index";

// 页面刷新时，重新赋值token
if (sessionStorage.getItem("token")) {
    store.commit("set_token", sessionStorage.getItem("token"));
}

//1.通过Vue.use(插件),安装插件
Vue.use(VueRouter);

//2.创建路由对象
const routes = [
    {
        path: "",
        redirect: "/home",
    },
    {
        path:"/personalCenter",
        redirect: "/personalCenter/main/baseInfo"
    }
,
    {
        path: "",
        name:"home",
        component: () => import("../views/home/Home"),
        meta: {
            title: "首页"
        },
        children: [
            {
                path: "/home",
                name:'main',
                component: () => import("../views/home/Main"),
                meta: {
                    title: '首页',
                },
            },
            {
                path: '/job',
                name:'job',
                component: () => import("../views/home/Job"),
                meta: {
                    title: '职位',
                },
            },
            {
                path: '/company',
                name:'company',
                component: () => import("../views/home/Company"),
                meta: {
                    title: '公司',
                },
            },
            {
                path: '/news',
                component: () => import("../views/home/News"),
                meta: {
                    title: '资讯',
                },
            },
            {
                path: '/schoolRecruit',
                component: () => import("../views/home/SchoolRecruit"),
                meta: {
                    title: '校园招聘',
                },
            },
            {
                path: '/resumeTemplate',
                component: () => import("../views/home/ResumeTemplate"),
                meta: {
                    title: '简历模板',
                },
            },
            {
                path: '/preach',
                component: () => import("../views/home/Preach"),
                meta: {
                    title: '企业宣讲',
                },
            },
            {
                path: '/community',
                component: () => import("../views/home/Community"),
                meta: {
                    title: '社区交流',
                },
            }, {
                path: '/development',
                component: () => import("../views/home/Development"),
                meta: {
                    title: '职业发展',
                },
            },{
                path: '/job/jobDetail',
                name:'jobDetail',
                component: () => import("../views/home/JobDetail"),
                meta: {
                    title: '职位详情',
                },
            },{
                path: '/company/companyDetail',
                name:'companyDetail',
                component: () => import("../views/home/CompanyDetail"),
                meta: {
                    title: '公司详情',
                },
            },{
                path: '/news/newsDetail',
                name:'newsDetail',
                component: () => import("../views/home/NewsDetail"),
                meta: {
                    title: '资讯详情',
                },
            }, {
                path: '/resumeTemplate/resumeTemplateDetail',
                name:'resumeTemplateDetail',
                component: () => import("../views/home/ResumeTemplateDetail"),
                meta: {
                    title: '简历模板详情',
                },
            }, {
                path: '/preach/preachLiveDetail',
                name:'preachLiveDetail',
                component: () => import("../views/home/PreachLiveDetail"),
                meta: {
                    title: '宣讲直播详情',
                },
            },{
                path: '/preach/preachVideoDetail',
                name:'preachVideoDetail',
                component: () => import("../views/home/PreachVideoDetail"),
                meta: {
                    title: '宣讲视频详情',
                },
            }, {
                path: '/community/article',
                name:'article',
                component: () => import("../views/home/Article"),
                meta: {
                    title: '社区文章',
                },
            }, {
                path: '/development/course',
                name:'course',
                component: () => import("../views/home/Course"),
                meta: {
                    title: '学习课程',
                },
            }, {
                path: '/userPage',
                name:'userPage',
                component: () => import("../views/home/UserPage"),
                meta: {
                    title: '用户主页',
                },
            },
        ]
    },
    {
        path: "/login",
        name: "login",
        component: () => import("../views/home/Login"),
        meta: {
            title: "登录"
        },
    },
    {
        path: '/personalCenter',
        name: "personalCenter",
        component: () => import("../views/person/PersonalCenter"),
        meta: {
            title: '个人中心',
            requireAuth:true,
        },
        children: [
            {
                path: "main",
                name: "main",
                component: () => import("../views/person/PersonalMain"),
                meta: {
                    title: "个人中心首页",
                    requireAuth:true,
                },
                children:[
                    {
                    path: "resume",
                    name:"resume",
                    component: () => import("../views/person/Resume"),
                    meta: {
                        title: "我的简历",
                        requireAuth:true,
                    },
                },{
                    path: "delivery",
                    name: "delivery",
                    component: () => import("../views/person/Delivery"),
                    meta: {
                        title: "投递管理",
                        requireAuth:true,
                    },
                },{
                    path: "writtenInterview",
                    name: "writtenInterview",
                    component: () => import("../views/person/WrittenInterview"),
                    meta: {
                        title: "笔试面试",
                        requireAuth:true,
                    },
                },
                    {
                        path: "jobCollect",
                        name: "jobCollect",
                        component: () => import("../views/person/JobCollect"),
                        meta: {
                            title: "职位收藏",
                            requireAuth:true,
                        },
                    },
                    {
                        path: "vipServer",
                        name: "vipServer",
                        component: () => import("../views/person/VipServer"),
                        meta: {
                            title: "会员服务",
                            requireAuth:true,
                        },
                    },
                    {
                        path: "articleManage",
                        name: "articleManage",
                        component: () => import("../views/person/ArticleManage"),
                        meta: {
                            title: "文章管理",
                            requireAuth:true,

                        },
                    },
                    {
                        path: "editArticle",
                        name: "editArticle",
                        component: () => import("../components/content/MarkDown"),
                        meta: {
                            title: "编辑文章",
                            requireAuth:true,
                        },
                    },
                    {
                        path: "message",
                        name: "message",
                        component: () => import("../components/content/Message"),
                        meta: {
                            title: "系统消息",
                            requireAuth:true,
                        },
                    },

                    {
                        path: "learnCourse",
                        name: "learnCourse",
                        component: () => import("../views/person/LearnCourse"),
                        meta: {
                            title: "学习课程",
                            requireAuth:true,
                        },
                    },{
                        path: "communication",
                        name: "communication",
                        component: () => import("../views/person/Communication"),
                        meta: {
                            title: "信息交流",
                            requireAuth:true,
                        },
                    }, {
                        path: "baseInfo",
                        name: "baseInfo",
                        component: () => import("../views/person/BaseInfo"),
                        meta: {
                            title: "基本信息",
                            requireAuth:true,
                        },
                    }
                ]
            },

        ]
    }

];

const router = new VueRouter({
    //配置路由和组件之间的关系
    routes,
    // mode: "history",
    linkActiveClass: "active",
});

// 前置守卫(guard)
router.beforeEach((to, from, next) => {
    if (to.matched.some((r) => r.meta.requireAuth)) {
        //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
        if (store.getters.token) {
            next();
        } else {
            Message.warning({message: '您还未登录，请先登录', duration: 1000})
            next({
                name: "login",
            });
        }
    } else {
        next();
    }
});

//后置钩子(hook)
router.afterEach((route) => {
    console.log(route.fullPath)
});

//解决两次访问相同路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

// 3.将router对象传到vue实例
export default router;

